<!-- 订单管理 -->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" inline class="ruleForm">
    <el-form-item label="车牌号：" prop="carNo">
      <el-input
          v-model="ruleForm.queryRo.carNo"
          style="width: 150px"
          clearable
          placeholder="请输入车牌号"
      />
    </el-form-item>
    <el-form-item label="车辆状态：" prop="status">
      <el-select v-model="ruleForm.queryRo.status" clearable style="width: 150px">
        <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
        />
      </el-select>
    </el-form-item>

    <el-form-item label="入库时间：" >
      <el-date-picker
          v-model="createtime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>
    <el-form-item label="车辆类型：" prop="carType">
      <el-input
        v-model="ruleForm.queryRo.carType"
        style="width: 150px"
        clearable
        placeholder="请输入车辆类型"
      />
    </el-form-item>
    <el-form-item label="车辆颜色：" prop="carColor">
      <el-input
          v-model="ruleForm.queryRo.carColor"
          style="width: 150px"
          clearable
          placeholder="请输入车辆颜色"
      />
    </el-form-item>

    <el-form-item>
      <el-radio-group v-model="ruleForm.queryRo.ifDriver" @change="radioChange">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button label="0">无司机</el-radio-button>
        <el-radio-button label="1">有司机</el-radio-button>
      </el-radio-group>
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
      <el-button type="success" @click="addCar()">新增</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableList" style="width: 100%" v-loading="loading">
    <el-table-column prop="carNo" label="车牌号" align="center" width="300px" />
    <el-table-column prop="status" label="车辆状态" align="center">
      <template #default="scope">
        <span >{{ statusNum(scope.row.status) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="入库时间" align="center" />
    <el-table-column prop="carType" label="车辆类型" align="center" />
    <el-table-column prop="carColor" label="车辆颜色" align="center" />
    <el-table-column prop="driverName" label="司机" align="center" />
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button link @click="openDialog(scope.row)" type="primary">查看详情</el-button>
        <el-button link type="primary" size="small" @click="editCar(scope.row)">编辑</el-button>
        <el-button link type="danger" size="small" @click="delCar(scope.row)">删除</el-button>
        <el-button link type="warning" size="small" @click="">下载二维码</el-button>
      </template>
    </el-table-column>
  </el-table>

  <footer class="pagination">
    <el-pagination
      v-model:currentPage="ruleForm.current"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      small
    />
  </footer>

  <el-drawer
      v-model="drawer"
      :title="tableForm.data.id ? '编辑设备资料': '新增设备资料'"
      :direction="direction">
    <el-form
        ref="ruleFormRef"
        :model="tableForm.data"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
    >
      <!--      vehicleNo:"",-->
      <!--      bmsNo:"",-->
      <!--      operatorId: "",-->
      <!--      operatorPerson: "",-->
      <!--      orgName: "",-->
      <el-form-item label="车牌号" prop="carNo">
        <el-input v-model="tableForm.data.carNo" placeholder="请填车牌号" />
      </el-form-item>
      <el-form-item label="车辆类型" prop="carType">
        <el-input v-model="tableForm.data.carType" placeholder="请填车辆类型" />
      </el-form-item>
      <el-form-item label="车辆颜色" prop="carColor">
        <el-input v-model="tableForm.data.carColor" placeholder="请填车辆颜色" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="flex: auto">
        <!--        <el-button @click="cancelForm">取消</el-button>-->
        <el-button type="primary"  @click="submitForm(ruleFormRef)">确定</el-button>
      </div>
    </template>
  </el-drawer>

  <carDetailDialog
      v-model:show="showDialog"
      :detail="detail"
      @openDialog="openDialog"
  ></carDetailDialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import carDetailDialog from './components/carDetailDialog.vue'
import api from '@/api/apis/carApi'
import {ElMessage, ElMessageBox, FormInstance, FormRules} from "element-plus";
const ruleForm = reactive({
  queryRo:{
    carColor:"",
    carType:"",
    createTime:"",
    carNo:"",
    ifDriver:"",
    // status:undefined
  },
  current: 1,
  pageSize: 50,
  searchCount: true
})
const time=ref<any>([])
const createtime=ref<any>('')

const drawer = ref(false)
const ruleFormRef = ref<FormInstance>()

//新增
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let res, message
      // ruleForm.data.sex = sex.value
      console.log('data',tableForm.data)
      if(tableForm.data.id){
        res = await api.updateCar(tableForm.data)
        message = '编辑成功！'
      }else {
        res = await api.addCar(tableForm.data)
        message = '新增成功！'
      }
      if(res.data.code == 200){
        ElMessage({
          message: message,
          type: 'success',
        })
        getTableList()
        drawer.value = false
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 新增
const addCar = async (formEl: FormInstance | undefined) => {
  drawer.value = true
  let  obj = {
    carType:"",
    carNo:"",
    carColor: "",
  }
  tableForm.data = obj
}

//编辑
const editCar = (row)=>{
  tableForm.data= {...row}
  // status.value = ruleForm.data.status == 1 ? true : false
  // ruleForm.data.status = 1
  drawer.value = true
  console.log('ruleForm',tableForm.data)
}

const tableForm = reactive({
  data:{
    id:"",
    carType:"",
    carNo:"",
    carColor: "",
  },
})

// 必填规则
const rules = reactive<FormRules>({
  carNo: [
    { required: true, message: '请填车牌号', trigger: 'blur' },
  ],
  carType: [
    { required: true, message: '请填车辆类型', trigger: 'blur' },
  ],
  carColor: [
    { required: true, message: '请填车辆颜色', trigger: 'blur' },
  ],
})

const delCar = (row)=>{
  ElMessageBox.confirm(
      '您确定要删除该车辆吗?',
      '注意',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let res = await api.delCarById(row.id)
        console.log(res)
        if(res.data.code == 200){
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getTableList()
        }

      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}


//改变筛选状态
const radioChange=()=>{
  ruleForm.current = 1
  getTableList()
}


//总数
const total = ref(0)
//搜索
const search = () => {
  ruleForm.current = 1
  getTableList()
}

const statusNum = (value: string) => {
  let obj: any = {
    0: '未使用',
    1: '使用中',
  }
  return obj[value]
}
const statusList=[
  {name:"未使用",value:0},
  {name:"使用中",value:1},
]

//表格数据
const tableList = ref([])
//加载
const loading = ref(false)
onMounted(()=>{
  getTableList()
})
//获取表格数据
const getTableList = async () => {
  loading.value = true
  try {
    let obj = { ...ruleForm }
    // obj.queryRo.chargeBeginTime =time.value?time.value[0]:undefined
    // obj.queryRo.chargeEndTime =time.value?time.value[1]:undefined
    obj.queryRo.createTime =createtime.value?createtime.value:undefined
    let {data} = await api.carPage(obj)
    tableList.value= data.data.rows
    total.value= Number(data.data.total)

  } catch (error) {
    loading.value = false
  }
  loading.value = false
}
// //改变每页条数
const handleSizeChange = (val: number) => {
  ruleForm.current = 1
  ruleForm.pageSize = val
  getTableList()
}
//改变当前页数
const handleCurrentChange = (val: number) => {
  ruleForm.current = val
  getTableList()
}

//弹框开关
const showDialog = ref(false)
//详情id
const detail = ref()
//打开详情弹框
const openDialog = (val: string) => {
  detail.value = val
  console.log('值---',val)
  showDialog.value = true
}

</script>

<style scoped lang="scss">
footer {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
